
var app = getApp();

const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../services/user.js');

var pageNo = 1;
var hadLastPage = false;
var isLoad = 0;

Page({
  data: {
    assessList: [],
    showAll: 1,
    showPic: 0,
  },
  onLoad: function (options) {
    this.setData({
      productStockId: options.productStockId
    });
  },
  onReady: function () {
    // 页面渲染完成

  },
  onShow: function () {
    // 页面显示
    pageNo = 1;
    hadLastPage = false;
    this.setData({
      assessList: []
    });
    this.getCounts();
    this.loadList();
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  //页面上拉触底执行的函数
  onReachBottom: function () {
    this.loadList();
  },
  //获取全部评价和有图评价数量
  getCounts: function () {
    var that = this;
    var data = {
      productStockId: this.data.productStockId
    }
    util.request(api.PinglunCounts, data, 'POST').then(function (data) {
      if (data.type == 'success') {
        that.setData({
          allCounts: data.data.allCounts,
          picCounts: data.data.picCounts
        });
      } else {
        console.log(data.content);
      }
    });
  },
  //加载列表
  loadList: function () {
    isLoad = 1;
    if (hadLastPage) {
      // wx.showToast({
      //   title: '到底了！',
      // });
      isLoad = 0;
      return;
    }
    var that = this;
    var data = {
      pageNo: pageNo,
      productStockId: this.data.productStockId,
      showAll: that.data.showAll,
      showPic: that.data.showPic
    }
    util.request(api.PinglunList, data, 'POST').then(function (data) {
      if (data.type == 'success') {
        var assessList = that.data.assessList;

        for (var i = 0; i < data.data.assessList.length; i++) {
          assessList.push(data.data.assessList[i]);
        }

        if (data.data.pageNo == data.data.totalPages) {
          hadLastPage = true;
        } else {
          pageNo++;
        }

        that.setData({
          assessList: assessList
        });
        isLoad = 0;
      } else {
        console.log(data.content);
        isLoad = 0;
      }
    });
  },
  switchAll: function () {
    if (isLoad == 1) {
      return;
    }
    this.setData({
      showAll: 1,
      showPic: 0,
      assessList: []
    });
    pageNo = 1;
    hadLastPage = false;
    this.loadList();
  },
  switchPic: function () {
    if (isLoad == 1) {
      return;
    }
    this.setData({
      showAll: 0,
      showPic: 1,
      assessList: []
    });
    pageNo = 1;
    hadLastPage = false;
    this.loadList();
  }
})